package com.lingkang.ztgateway.config.auth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.ReactiveAuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.web.server.authorization.AuthorizationContext;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author linke
 * @date 2020/11/13 0:29
 * @description
 */
@Component
@Slf4j
public class AuthorizationManager implements ReactiveAuthorizationManager<AuthorizationContext> {

  @Autowired
  private RemoteTokenServices remoteTokenServices;

  @Override
  public Mono<AuthorizationDecision> check(Mono<Authentication> mono,
      AuthorizationContext authorizationContext) {
    System.out.println(authorizationContext);
    ServerHttpRequest request = authorizationContext.getExchange().getRequest();
    String path = request.getURI().getPath();
    String token = request.getHeaders().getFirst("token");
    System.out.println(path);
    System.out.println(token);

    OAuth2AccessToken o=remoteTokenServices.readAccessToken(token);
    System.out.println(o);



    return Mono.just(new AuthorizationDecision(true));
//    return null;
  }
}
